import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Gallery from '../views/Gallery'
import Login from '../views/Login'
import Register from '../views/Register'
import Profile from '../views/Profile'
import Page404 from '../components/Page404'
import Collect from '../components/profile/Collect'
import Post from '../components/profile/Post'
import AccountSetting from '../components/profile/AccountSetting'
import SecuritySetting from '../components/profile/SecuritySetting'
import Notification from '../components/profile/Notification'
import ImgList from '../views/ImgList'
import Admin from '../views/Admin'
import store from '../store'
import Follow from '../components/profile/Follow'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Index',
    component: Home
  },
  {
    path: '/home',
    name: 'Home',
    component: Home
  },
  {
    path: '/category',
    name: 'Category',
    component: () => import(/* webpackChunkName: "about" */ '../views/Category.vue')
  },
  {
    path: '/category/:name',
    name: 'ImgList',
    component: ImgList
  },
  {
    path: '/gallery',
    name: 'Gallery',
    component: Gallery
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/profile',
    name: 'Profile',
    redirect: 'myCollect',
    component: Profile,
    children: [
      {
        path: '/myCollect',
        name: 'MyCollect',
        component: Collect,
        meta: { requiresAuth: true }
      },
      {
        path: '/myPost',
        name: 'MyPost',
        component: Post,
        meta: { requiresAuth: true }
      },
      {
        path: '/accountSetting',
        name: 'AccountSetting',
        component: AccountSetting,
        meta: { requiresAuth: true }
      },
      {
        path: '/securitySetting',
        name: 'SecuritySetting',
        component: SecuritySetting,
        meta: { requiresAuth: true }
      },
      {
        path: '/notification',
        name: 'Notification',
        component: Notification,
        meta: { requiresAuth: true }
      },
      {
        path: '/follow',
        name: 'Follow',
        component: Follow,
        meta: { requiresAuth: true }
      }

    ]
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    meta: { requiresAdmin: true }
  },
  {
    // 会匹配所有路径
    path: '*',
    name: '404',
    component: Page404
  }

  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  // component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')

]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.state.loggedIn) {
      next({
        path: '/login'
      })
    } else {
      next()
    }
  } else if (to.matched.some(record => record.meta.requiresAdmin)) {
    if (store.state.userInfo.role !== 'admin') {
      next({
        path: '/home'
      })
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
